package com.runvp.service.app;

import com.alibaba.fastjson.JSONObject;
import com.huifu.adapay.core.AdapayCore;
import com.huifu.adapay.core.util.AdapaySign;
import com.runvp.common.enums.PayStatus;
import com.runvp.common.model.bo.pay.PayCallbackBO;
import com.runvp.service.biz.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * app Service业务层处理
 *
 * @author runvp
 * @date 2022-09-18
 */
@Slf4j
@Service
public class AppPayService {

    @Autowired
    private BizSiteMapper bizSiteMapper;
    @Autowired
    private BizCustomerMapper bizCustomerMapper;
    @Autowired
    private BizDeviceMapper bizDeviceMapper;
    @Autowired
    private BizGoodsMapper bizGoodsMapper;

    @Autowired
    private BizOrderMapper bizOrderMapper;

    /**
     * 支付回调处理
     *
     * @param request
     */
    public void handleCallback(HttpServletRequest request) {
        try {
            //验签请参data
            String data = request.getParameter("data");
            //验签请参sign
            String sign = request.getParameter("sign");
            PayCallbackBO bo = buildPayCallbackBO(request);
            log.info("支付回调:::bo:{}", JSONObject.toJSONString(bo));
            if (PayStatus.PAY_SUCCEEDED.getCode().equals(bo.getType())) {
                log.info("支付回调:::处理【{}】业务", PayStatus.PAY_SUCCEEDED.getDesc());

            }
            if (PayStatus.PAY_FAILED.getCode().equals(bo.getType())) {
                log.info("支付回调:::处理【{}】业务", PayStatus.PAY_FAILED.getDesc());

            }
            if (PayStatus.PAY_CLOSE_SUCCEEDED.getCode().equals(bo.getType())) {
                log.info("支付回调:::处理【{}】业务", PayStatus.PAY_CLOSE_SUCCEEDED.getDesc());

            }
            if (PayStatus.PAY_CLOSE_FAILED.getCode().equals(bo.getType())) {
                log.info("支付回调:::处理【{}】业务", PayStatus.PAY_CLOSE_FAILED.getDesc());

            }
            data = bo.getData();
            sign = bo.getSign();
            //验签标记
            boolean checkSign;
            //验签请参publicKey
            String publicKey = AdapayCore.PUBLIC_KEY;
            log.info("支付回调:::验签请参：data={},sign={}", data, sign);
            //验签
            checkSign = AdapaySign.verifySign(data, sign, publicKey);
            if (checkSign) {
                //验签成功逻辑
                log.info("支付回调:::成功返回数据data:{}", data);

            } else {
                //验签失败逻辑
                log.error("支付回调:::失败数据data:{}", data);
            }
        } catch (Exception e) {
            log.error("支付回调:::处理异常，request:{}", e);
        }
    }

    /**
     * 解析构建支付对象
     *
     * @param request
     * @return PayCallbackBO
     */
    private PayCallbackBO buildPayCallbackBO(HttpServletRequest request) {
        PayCallbackBO bo = new PayCallbackBO();
        bo.setCreatedTime(request.getParameter("created_time"));
        bo.setData(request.getParameter("data"));
        bo.setProdMode(request.getParameter("prod_mode"));
        bo.setSign(request.getParameter("sign"));
        bo.setId(request.getParameter("id"));
        bo.setType(request.getParameter("type"));
        bo.setAppId(request.getParameter("app_id"));
        bo.setObject(request.getParameter("object"));
        return bo;
    }

}
